home *** CD-ROM | disk | FTP | other *** search
Text File | 1984-12-19 | 6.3 KB | 288 lines | [TEXT/PASC] |
- program Regions;
-
- { Regions is a MacPascal demostration program}
- { that provides six examples of the workings of}
- { the regions of QuickDraw. }
- { -- by Chris Derossi}
-
- uses
- QuickDraw2; {QuickDraw2 contains the stuff for regions.}
-
- const
- { These constants determine the amount and density of the}
- { words used to show region clipping}
-
- WordsPerLine = 6;
- NumLines = 10;
- LineHite = 15;
-
- var
- My_Rgn : RgnHandle; { This is our working region, used everywhere}
- Big_Rect : Rect; { This is a rectangle that is the whole drawing window}
-
- procedure SetUp;
-
- { SetUp clears the screen to a full sized drawing window and uses}
- { DrawLine to mark it off into six sections. It also inits our region}
- { and sets Big_Rect to the full window.}
-
- begin
- HideCursor;
- HideAll;
- SetRect(Big_Rect, 0, 20, 527, 357); {Full screen size}
- SetDrawingRect(Big_Rect);
- ShowDrawing; {Show only the drawing window}
- DrawLine(0, 149, 527, 149);
- DrawLine(176, 0, 176, 337);
- DrawLine(352, 0, 352, 337);
- My_Rgn := NewRgn; {Init our working region}
- end;
-
- procedure DrawWords (X, Y : integer);
-
- { DrawWords draw several lines of words starting at (X,Y) according}
- { to globally declared constants}
-
- var
- s : string;
- a, b : integer;
-
- begin
- s := 'Regions ';
- for a := 0 to NumLines do
- begin
- MoveTo(X, Y + a * LineHite); {Start a new line in column Y}
- for b := 1 to WordsPerLine do
- DrawString(s);
- end;
- end;
-
- procedure Use_Region;
-
- { This procedure draws an outline of our region, then shrinks it so that}
- {the outline is not in the region, and sets the drawing window's clipping}
- {region to equal our region}
-
- begin
- FrameRgn(My_Rgn); { Draw the outline of our region}
- InsetRgn(My_Rgn, 1, 1); { Shrink it}
- SetClip(My_Rgn); { Set the drawing window's clipping region}
- end;
-
- procedure ClearRgn;
-
- { In order to use the entire drawing window, this procedure sets the}
- {clipping region to the whole window, using Big_Rect}
-
- begin
- RectRgn(My_Rgn, Big_Rect); {Make our region a rectangle, screen size.}
- SetClip(My_Rgn);
- end;
-
- procedure DoRegion1;
-
- { This creates the first region, which is just a circle}
-
- var
- r : Rect;
-
- begin
- ClearRgn;
- MoveTo(25, 140);
- DrawString('Simple Region');
-
- begin {Create a region}
- OpenRgn;
- SetRect(r, 20, 20, 120, 120);
- FrameOval(r);
- CloseRgn(My_Rgn);
- end;
-
- Use_Region;
- DrawWords(0, 0);
- end;
-
- procedure DoRegion2;
-
- { This is region number 2. This is a concatination of 3 shapes.}
-
- var
- r : Rect;
-
- begin
- ClearRgn;
- MoveTo(225, 140);
- DrawString('Any Shape');
-
- OpenRgn; {Start a new region}
- SetRect(r, 220, 65, 300, 85);
- FrameRect(r);
- SetRect(r, 180, 30, 220, 120);
- FrameOval(r);
- SetRect(r, 300, 30, 340, 120);
- FrameOval(r);
- CloseRgn(My_Rgn);
-
- Use_Region;
- DrawWords(176, 0);
- end;
-
- procedure DoRegion3;
-
- { Region number 3. Two overlapping regions form a 'hole'.}
-
- var
- r : Rect;
-
- begin
- ClearRgn;
- MoveTo(410, 140);
- DrawString('Holes');
-
- OpenRgn;
- SetRect(r, 380, 20, 480, 120);
- FrameOval(r);
- SetRect(r, 410, 50, 450, 90); {Completely inside the first shape}
- FrameRoundRect(r, 18, 18);
- CloseRgn(My_Rgn);
-
- Use_Region;
- DrawWords(352, 0);
- end;
-
- procedure DoRegion4;
-
- { Region 4. This creates a region which shows the effect of }
- {partially overlapping region areas.}
-
- var
- r : Rect;
-
- begin
- ClearRgn;
- MoveTo(40, 310);
- DrawString('Overlapping');
-
- OpenRgn;
- SetRect(r, 30, 180, 130, 280);
- FrameOval(r); { Draw a simple circle }
- SetRect(r, 5, 215, 155, 245);
- FrameRect(r); { Draw a rectangle over the circle }
- CloseRgn(My_Rgn);
-
- Use_Region;
- DrawWords(0, 149);
- end;
-
- procedure DoRegion5;
-
- { Region 5. Regions do not have to be contiguous. This procedure creates}
- {a region with three separate areas.}
-
- var
- r : Rect;
-
- begin
- ClearRgn;
- MoveTo(220, 310);
- DrawString('Disjoint Areas');
-
- OpenRgn;
- SetRect(r, 190, 160, 270, 230);
- FrameOval(r);
- SetRect(r, 280, 160, 340, 240);
- FrameRect(r);
- SetRect(r, 190, 255, 340, 285);
- FrameRoundRect(r, 18, 18);
-
- CloseRgn(My_Rgn);
- Use_Region;
- DrawWords(176, 150);
- end;
-
- procedure DoRegion6;
-
- { Region 6. This procedure illustrates the effect of continuous clipping}
- {associated with any drawing, even animation.}
-
- var
- r : Rect;
-
- procedure Animate;
-
- { Animate, called only by DoRegion6, bounces a ball in the general area}
- {of region 6, showing that the clipping occurs constantly, with now need}
- {for special instructions. The animation stops when the mouse button is}
- {pressed.}
-
- var
- X, Y, dX, dY : integer;
-
- begin
- X := 353; { Arbitrary starting position. }
- Y := 220;
- dX := 3; { Arbitrary beginning velocity. }
- dY := 0;
- repeat
- PenPat(white); { Use white for drawing to }
- PaintCircle(X, Y, 5); { erase ball at current position }
- X := X + dX; { upgrade position }
- Y := Y + dY;
- if (X < 353) or (X > 510) or (Y < 175) or (y > 265) then
- begin { The ball has hit a "wall" and should bounce }
- X := X - dX; { Move the ball back to its last legal position }
- Y := Y - dY;
- repeat
- dX := ((random mod 3) - 1) * 7; { Choose new random velocities,}
- dY := ((random mod 3) - 1) * 7; {with each being -7,0, or 7 }
- until (dX <> 0) or (dY <> 0); { zero velocity is illegal }
- end
- else { new ball position is okay. }
- begin
- PenPat(black);
- PaintCircle(X, Y, 5); { Draw the ball in the new position }
- end;
- until Button; { Stop when the button is pressed }
- end;
-
- begin { Region6 }
- ClearRgn;
- SetRect(r, 353, 150, 530, 290);
- FillRect(r, gray); { Use a gray background for contrast }
- MoveTo(375, 310);
- DrawString('Continuous Clipping');
-
- OpenRgn;
- SetRect(r, 357, 185, 432, 255);
- FrameOval(r);
- SetRect(r, 432, 185, 507, 255);
- FrameOval(r);
- CloseRgn(My_Rgn);
-
- Use_Region;
- SetRect(r, 353, 150, 530, 290);
- FillRect(r, white); { Erase the inside of the region, using auto-clipping}
- Animate; { Do the bouncing ball }
- end;
-
- procedure MakeRgns;
-
- { This is a brute force way to call all six regions, but it allows us to}
- {break the regions down into separate procedures.}
-
- begin
- DoRegion1;
- DoRegion2;
- DoRegion3;
- DoRegion4;
- DoRegion5;
- DoRegion6;
- ClearRgn;
- DisposeRgn(My_Rgn); { Free the memory used for our region }
- ShowCursor; { We need the cursor! }
- end;
-
- begin { Regions }
- SetUp;
- MakeRgns;
- end.